package com.iman.locker.view.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.iman.locker.R;


/**
 * 数字键盘
 * Created by esirong on 2015/11/13.
 */
public class PinNumericKeyboard extends LinearLayout implements View.OnClickListener {

    private CheckBox Pin1;
    private CheckBox Pin2;
    private CheckBox Pin3;
    private CheckBox Pin4;
    private boolean resetPinCode = true;
    String password = "";
    private Handler handler = new Handler();
    /**
     * 结果回调监听器接口
     */
    private CallBack callBack;

    public PinNumericKeyboard(Context context) {
        this(context, null);
    }

    public PinNumericKeyboard(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PinNumericKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public PinNumericKeyboard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View.inflate(context, R.layout.view_pin_number_keyboard, this);
        Pin1 = (CheckBox) findViewById(R.id.checkBox1);
        Pin2 = (CheckBox) findViewById(R.id.checkBox2);
        Pin3 = (CheckBox) findViewById(R.id.checkBox3);
        Pin4 = (CheckBox) findViewById(R.id.checkBox4);
        NumericKeyboard numericKeyboard = (NumericKeyboard) findViewById(R.id.numeric_keyboard2);
        numericKeyboard.setCallBack(new NumericKeyboard.CallBack() {
            @Override
            public void onInput(int number) {
                if (resetPinCode) {
                    if (password.length() < 4) {
                        password = password + number;
                        setPinCodeView(password);
                        //不可以再输入1秒钟之内
                    }
                    if (password.length() == 4) {
                        doAuthorize(password);
                        resetPinCode = false;
                        handler.postDelayed(dismissPinCode, 1000);
                    }
                }

            }
        });

    }

    //重置pin码
    private Runnable dismissPinCode = new Runnable() {
        @Override
        public void run() {
            resetPinCodeView();
            password = "";
            resetPinCode = true;
        }
    };

    private void setPinCodeView(String num) {
        if (num.length() >= 4) {
            Pin1.setChecked(true);
            Pin2.setChecked(true);
            Pin3.setChecked(true);
            Pin4.setChecked(true);
        } else if (num.length() >= 3) {
            Pin1.setChecked(true);
            Pin2.setChecked(true);
            Pin3.setChecked(true);
        } else if (num.length() >= 2) {
            Pin1.setChecked(true);
            Pin2.setChecked(true);

        } else if (num.length() >= 1) {
            Pin1.setChecked(true);

        }
    }

    private void resetPinCodeView() {
        Pin1.setChecked(false);
        Pin2.setChecked(false);
        Pin3.setChecked(false);
        Pin4.setChecked(false);
    }

    public void startAnimate() {
        Animation shake = AnimationUtils.loadAnimation(getContext(), R.anim.shake);
        findViewById( R.id.pin_code_layout ).startAnimation( shake );
    }

    private void doAuthorize(String password) {
        if (callBack != null) {
            callBack.onInput(password);
        }
    }

    @Override
    public void onClick(View v) {

    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public interface CallBack {
        void onInput(String password);
    }
}
